package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Markdown;
import com.example.demo.entity.QuestionCenter;
import com.example.demo.mapper.MarkdownMapper;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/markdown")
public class MarkdownController {

    @Resource
    MarkdownMapper markdownMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Markdown markdown) {
        if(markdown.getTitle().equals("")) {
            return Result.error("-1","请补充文章标题");
        } // 标题为空则提醒
        markdown.setTime(new Date());
        markdownMapper.insert(markdown);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        markdownMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}") // 通过Id查找
    public Markdown findById(@PathVariable long id) {
        Markdown markdown = markdownMapper.selectById(id);
        return markdown;
    }


    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Markdown> wrapper = Wrappers.<Markdown>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(Markdown::getAuthor,search);
        }
        Page<Markdown> markdownPage = markdownMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(markdownPage);
    }

    // 导出Excel
    @ResponseExcel(name = "markdown", sheet = "markdown")
    @GetMapping("/downloadExcel")
    public List<Markdown> DownloadExcel(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "1000") Integer pageSize,
                                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Markdown> wrapper = Wrappers.<Markdown>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Markdown::getTitle,search);
        }
        Page<Markdown> markdownPage = markdownMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<Markdown> records = markdownPage.getRecords();
        return records;
    }

    @PutMapping //改
    public Result<?> update(@RequestBody Markdown markdown) {
        markdownMapper.updateById(markdown);
        return Result.success();
    }
}
